// --- Copyright (c) 2006-2008 Stefan Kyntchev ---
// This software is written and copyrighted by Stefan Kyntchev 
// and BeyondPod Team members. All rights are reserved.
// Author contact: support@beyondpod.mobi
// ------------------------------------------------------------------------
// This file is part of BeyondPod RSS Feed Reader and Podcast manager
// (www.codeplex.com/BeyondPod) 
// BeyondPod is free software: you can redistribute it and/or modify 
// it under the terms of the GNU General Public License as published by 
// the Free Software Foundation, either version 3 of the License, or 
// (at your option) any later version. 
//  
// BeyondPod is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty of 
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
// GNU General Public License for more details. 
//  
// You should have received a copy of the GNU General Public License
// along with BeyondPod. If not, see <http://www.gnu.org/licenses/>
// ------------------------------------------------------------------------
// COPYRIGHT NOTICE: This copyright notice may NOT be removed, obscured or modified 
// without written consent from the author.
// --- End copyright notice --- 


using System.Collections.Generic;
using System.Diagnostics;
using System.Xml;
using svs.Mobile.BeyondMedia.FeedCore.RSS.Parsers;

namespace svs.Mobile.BeyondMedia.FeedCore.SysIntegration
{

	#region Child Classes

	public class GObject
	{
		private string _ID;
		private string _Label;
		private GObjectList _List;
		private string _Title;

		public string Label
		{
			get { return _Label; }
			set { _Label = value; }
		}

		public string Title
		{
			get { return _Title; }
			set { _Title = value; }
		}

		public string ID
		{
			get { return _ID; }
			set { _ID = value; }
		}

		public GObjectList List
		{
			get { return _List; }
			set { _List = value; }
		}

		public string GetIDAsUrl()
		{
			if (ID.Length > 5)
				return ID.Substring(5);
			else
				return null;
		}
	}

	public class GObjectList : List<GObject>
	{
		private string _Name;

		public string Name
		{
			get { return _Name; }
			set { _Name = value; }
		}
	}

	#endregion

	internal class GoogleFeedListParser : ParserBase
	{
		private readonly Dictionary<int, GObject> m_ObjectDictionary = new Dictionary<int, GObject>();
		private readonly Dictionary<int, GObjectList> m_ObjectListDictionary = new Dictionary<int, GObjectList>();

		public GoogleFeedListParser()
		{
			m_handlers["object"] = ObjectHandler;
			m_handlers["list"] = ListHandler;
			m_handlers["string"] = StringHandler;
		}

		public GObject RootObject
		{
			get { return m_ObjectDictionary.ContainsKey(0) ? m_ObjectDictionary[0] : null; }
		}

		private void ObjectHandler(XmlReader aReader)
		{
			GObjectList parent = GetParentList(aReader);
			GObject go = new GObject();
			m_ObjectDictionary[aReader.Depth] = go;

			if (parent != null)
			{
				parent.Add(go);
			}
		}

		private void ListHandler(XmlReader aReader)
		{
			GObject parent = GetParentObject(aReader);
			GObjectList gol = new GObjectList();
			m_ObjectListDictionary[aReader.Depth] = gol;

			if (parent != null)
			{
				parent.List = gol;
			}
			AttributeCollection att = ReadAttributes(aReader);
			string listName = string.Empty;

			if (att.ContainsKey("name"))
				listName = att["name"];

			gol.Name = listName;
		}

		private void StringHandler(XmlReader aReader)
		{
			GObject parent = GetParentObject(aReader);
			Debug.Assert(parent != null);

			AttributeCollection att = ReadAttributes(aReader);

			if (att.ContainsKey("name"))
			{
				switch (att["name"])
				{
					case "id":
						parent.ID = ReadElementString(aReader);
						break;

					case "title":
						parent.Title = ReadElementString(aReader);
						break;

					case "label":
						parent.Label = ReadElementString(aReader);
						break;
				}
			}
		}

		private GObject GetParentObject(XmlReader aReader)
		{
			if (m_ObjectDictionary.ContainsKey(aReader.Depth - 1))
				return m_ObjectDictionary[aReader.Depth - 1];
			else
				return null;
		}

		private GObjectList GetParentList(XmlReader aReader)
		{
			if (m_ObjectListDictionary.ContainsKey(aReader.Depth - 1))
				return m_ObjectListDictionary[aReader.Depth - 1];
			else
				return null;
		}
	}
}